<?php
// 把wechat的openid等调用起来

namespace GMantuWeChat;

use GMantuWeChat\Common;

session_start();
//引入配置文件
include_once __DIR__.'/../config.php';
//引入自动载入函数
include_once __DIR__.'/../autoloader.php';
//调用自动载入函数
AutoLoader::register();
?>

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>创建纪念日</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" >
<script language="javascript" src="../js/calendar/jquery-1.12.0.js"></script>
<link href="../resource/css/bootstrap.min.css" rel="stylesheet">

<script type="text/javascript">

window.onload = function(){
    // 初始化年月日
    for(i=1980; i<2050; i++)
        $("#time_year").append("<option value=\"" + i + "\">" + i + " </option>");
    $("#time_year").val("2016");    // 暂时不取本地时间了
    
    for(i=1; i<13; i++)
        $("#time_month").append("<option value=\"" + i + "\">" + i + " </option>");
    $("#time_month").val("3");    // 暂时不取本地时间了
    
    for(i=1; i<32; i++)
        $("#time_day").append("<option value=\"" + i + "\">" + i + " </option>");
    $("#time_day").val("1");    // 暂时不取本地时间了
    
    changeDay('y');
}

function changeDay(kind) {
    $("#time_year").show();
    $("#time_month").show();
    $("#time_day").show();
    
    if(kind=="y") {
        $("#time_year").hide();
    }
    else if(kind=="m") {
        $("#time_year").hide();
        $("#time_month").hide();
    }
}

function submit_dt() {
    format = $("input:radio[name='date_format']:checked").val();
    var error = 0;
    month = parseInt($("#time_month").val(), 10);
    day  = parseInt($("#time_day").val(), 10);
    
    if(format=='0') {  // 公历
        if((month==2 && day > 29) || day>31)   
            error = 1;
        else if (month==4||month==6||month==9||month==11){
            if(day>30)
                error = 2;    
        }
    }
    else {  // 阴历
        if(day>30)
            error = 3;
    }
    
    if($("#day_title").val() == "") {
        alert("请输入纪念日名称");
        return; 
    }
    
    if(error != 0) {
        alert("纪念日的日期不正确，请重新填写");
    }
    else {   
        $("#subform").submit();
    }  
}
</script>

<h1>创建纪念日</h1>
</head>
<body>
    
<form action="../control/add_event.php" method="post" id="subform">
    <div>
        <h3 class="demo-panel-title">纪念日名称
            <font size="3" color="#919191" style="font-style:italic;"> (比如“老婆生日”) </font>
        </h3>
        
        <div class="span3">
        <input type="text" value="" placeholder="结婚纪念日" class="span3" id="day_title" name="day_title" required>
        </div>
    </div>
    
    <div class="span3">
        
        <div>
        <label>
        <input type="radio" name="recycle" class="year_recycle" value="year" checked="checked" onchange="changeDay('y')">
        周年纪念 <font size="3" color="#919191" style="font-style:italic;"> (比如“生日、结婚纪念日”) </font>
        </label>
        </div>
        
        <div>
        <label>
        <input type="radio" name="recycle" class="month_recycle" value="month" onchange="changeDay('m')">
        月度纪念<font size="3" color="#919191" style="font-style:italic;"> (比如“交房租”)</font>
        </label>
       </div>
       
       <div>
        <label>
        <input type="radio" name="recycle" class="once_recycle" value="once"  onchange="changeDay('d')">
       单次纪念<font size="3" color="#919191" style="font-style:italic;"> (只会显示一次)</font>
        </label>
        </div>
        
         <div class="field-content">
        
        <select id="time_year" name="time_year">
        </select>
        
        <select id="time_month" name="time_month">
        </select>
        
        <select id="time_day" name="time_day">
        </select>
        </div>
        
         <label>
        <input type="radio" name="date_format" value="0" checked="checked">
        公历
        </label>
        
        <label>
        <input type="radio" name="date_format" value="1">
        农历
        </label>
        
        
        
    </div>
        
    <div class="form-group">
        <label class="field-label"> 描述 </label>
        <font size="3" color="#919191" style="font-style:italic;"> (比如“要送项链，不只是花！”) </font>
        <div class="field-content">
        <input type="text" value="" name="day_desc" id="day_desc">
        </div>
    </div>
    
    
    <input type="hidden" id="user_openid" name="user_openid" value="" />
    
    <div>
    <input type="button" class="btn btn-primary btn-success" onClick="submit_dt()" value="提交">
    </div>
    
    <div>
    <font size="3" color="#919191" style="font-style:italic;"> 数据永久保存在云端服务器，不用担心丢失。 </font>
    </div>
   

<?php

// 获取openid与userinfo，如果获取不到，说明没有加，则返回错误
// 如果没有获取到，说明是非微信访问或者其他非正常分享途径过来的，不强求。不显示其自定义信息即可
if (isset($_GET['code']))
{
    $token = \GMantuWeChat\Common\WeChatOAuth::getAccessTokenAndOpenId($_GET['code']);
    // $ret = \GMantuWeChat\Common\WeChatOAuth::getUserInfo($token['access_token'], $token['openid']);

    $openid = $token['openid'];
    echo <<<EOT
        <script type="text/javascript">
        document.getElementById("user_openid").value = "$openid";
        
        </script>
EOT;
}
else {
    echo <<<EOT
        <meta charset='utf-8'>
        <script type="text/javascript">
        alert("页面失效，请重新建立纪念日！");
        //location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxab31157c01de5423&redirect_uri=http://hmtbzx.com/calendar/index.php&response_type=code&scope=snsapi_base&state=123&connect_redirect=1#wechat_redirect";
        </script>
EOT;
}

?>

</body>
</html>
